// pages/addRun/addRun.js
let app = getApp(); 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSubmit: false,
    startLon:1,
    startLat:1,
    startAddress: "",
    endLon:1,
    endLat:1,
    endAddress:"",
    warn: "",
    text: "",
    imgs: [],   
    path:"",
    count: 3,
    "select_type":false,
    "type":"--请选择--",
    "types": ['外卖', '打印','雨伞','快递']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  bingInvite(){
    wx.navigateTo({
      url: '/pages/recommend/recommend'
    })
  },


  bindShowMsg_type() {
    this.setData({
      select_type: true
    })
  },
  mySelect_type(e) {
    console.log(e)
    var name = e.currentTarget.dataset.name
    this.setData({
      type: name,
      select_type: false
    })
  },

  jump(e){
    let isEnd = e.currentTarget.dataset.isend
    console.log(e)
    wx.navigateTo({
      url: '/pages/chooseLocation/chooseLocation?isEnd=' + isEnd,
    })
  },

  formSubmit: function (e) {
    let that=this
    var header={};
    header = { 
      'content-type': 'application/json'
    };
    var cookie = wx.getStorageSync("cookieKey");
    header['cookie'] = cookie;
    console.log('求跑腿发生了submit事件，携带数据为：', this.data.text+this.data.path+this.data.endLat+this.data.startLat);
    if (!this.data.text) {
      this.setData({
        warn: "发布内容不可为空",
        isSubmit: true
      })
      return;
    }
    wx.request({
      url: 'http://localhost:8080/bagman',
      method:"POST",
      header:header,
      contentType: "application/json",
      data:({
        "startLat":that.data.startLat,
        "startLon":that.data.startLon,
        "endLat":that.data.endLat,
        "endLon":that.data.endLon,
        "description":that.data.text,
        "status":"0"
      })
    })
  },

  bindUpload: function (e) {
    switch (this.data.imgs.length) {
      case 0:
        this.data.count = 3
        break
      case 1:
        this.data.count = 2
        break
      case 2:
        this.data.count = 1
        break
    }
    var that = this
    wx.chooseImage({
      count: that.data.count, // 默认3
      sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths

        that.setData({
          imgs:tempFilePaths,
        })
        var header={};
        header = { 
        'content-type': 'multipart/form-data'
        };
        var cookie = wx.getStorageSync("cookieKey");
        header['cookie'] = cookie;

        for (var i = 0; i < tempFilePaths.length; i++) {
          console.log(that.data.imgs[i])
          wx.uploadFile({
            url: 'http://localhost:8080/file/upload',
            filePath: tempFilePaths[i],
            name: "file",
            header:header,
            success: function (res) {
              console.log(res.data)
              console.log(JSON.parse(res.data).data)
              that.setData({
                path:JSON.parse(res.data).data
              })
              if (res.statusCode == 200) {
                wx.showToast({
                  title: "上传成功",
                  icon: "none",
                  duration: 1500
                })
  
                that.data.imgs.push(JSON.parse(res.data).data)
  
                // that.setData({
                //   imgs: that.data.imgs
                // })
              }
            },
            fail: function (err) {
              wx.showToast({
                title: "上传失败",
                icon: "none",
                duration: 2000
              })
            },
            complete: function (result) {
              console.log(result.errMsg)
            }
          })
        }
      }
    })
  },
  // 删除图片
  deleteImg: function (e) {
    var that = this
    wx.showModal({
      title: "提示",
      content: "是否删除",
      success: function (res) {
        if (res.confirm) {
          for (var i = 0; i < that.data.imgs.length; i++) {
            if (i == e.currentTarget.dataset.index) that.data.imgs.splice(i, 1)
          }
          that.setData({
            imgs: that.data.imgs
          })
        } else if (res.cancel) {
          console.log("用户点击取消")
        }
      }
    })
  }
})